﻿using System;
using System.Collections.Generic;
using System.Activities;
using System.Activities.XamlIntegration;

namespace Vision.Core.Components
{
    /// <summary>
    /// 
    /// </summary>
    public class ActivityExecuter : IDisposable
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="visionActivity"></param>
        /// <param name="inputParameters"></param>
        public void ExecuteVisionActivity(Entities.Activities.VisionActivity visionActivity, Entities.Activities.VisionActivityParameterCollection inputParameters)
        {
            Entities.Activities.VisionActivityParameterCollection outputParameters = null;
            this.ExecuteVisionActivity(visionActivity, inputParameters, out outputParameters);    
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="visionActivity"></param>
        /// <param name="inputParameters"></param>
        /// <param name="outputParameters"></param>
        public void ExecuteVisionActivity(Entities.Activities.VisionActivity visionActivity, Entities.Activities.VisionActivityParameterCollection inputParameters, out Entities.Activities.VisionActivityParameterCollection outputParameters)
        {
            DynamicActivity dynamicActivity = (DynamicActivity)ActivityXamlServices.Load(visionActivity.Activity.CreateReader());
            outputParameters = new Entities.Activities.VisionActivityParameterCollection();
            
            IDictionary<string, object> outDictionary = new Dictionary<string, object>();           
            Console.WriteLine("Executing Activity:" + dynamicActivity.Name);
            Console.WriteLine("Start:" + DateTime.Now.ToString());

            WorkflowInvoker workflowInvoker = new WorkflowInvoker(dynamicActivity);            
            outDictionary = workflowInvoker.Invoke(inputParameters.ToDictionary());
                        
            foreach (string key in outDictionary.Keys){
                outputParameters.Add(key, outDictionary[key]);
            }
            
            Console.WriteLine("End:" + DateTime.Now.ToString());
            
            workflowInvoker = null;
        }
                        
        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            
        }        
    }
}
